<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="../../js/jquery-3.3.1.min.js" charset="utf-8"></script>
    <link rel="stylesheet" href="/layui-v2.4.5/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<blockquote class="layui-elem-quote layui-text">
    关联优惠券列表
</blockquote>

<%--<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">--%>
<%--<legend>添加代理商</legend>--%>
<%--</fieldset>--%>

<form class="layui-form" id="myForm" action="###">
    <input type="hidden" name="id" value="${id}">

    <div class="layui-form-item">
        <label class="layui-form-label">优惠券</label>
        <div class="layui-input-block">
            <select name="couponId" lay-filter="aihao">
                <option value=""></option>
                <c:forEach items="${list}" var="co">
                <option value="${co.id}">${co.name}</option>
                </c:forEach>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="demo1">设置关联</button>

        </div>
    </div>
</form>




<script src="/layui-v2.4.5/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });
        //日期时间选择器
        laydate.render({
            elem: '#test1'
            ,type: 'datetime'
        });
        //日期时间选择器
        laydate.render({
            elem: '#test2'
            ,type: 'datetime'
        });

        //时间范围
        laydate.render({
            elem: '#test9'
            ,type: 'time'
            ,range: true
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '标题至少得5个字符啊';
                }
            }
            ,pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function(data){
            layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        //监听提交
        form.on('submit(demo1)', function(data){
            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })
            // return false;

            $.ajax({
                url: '/shop/shoplinkCoupon',
                type: 'POST', //GET
                async: true,    //或false,是否异步
                data: $("#myForm").serialize(),
                timeout: 5000,    //超时时间
                dataType: 'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend: function (xhr) {
                    /*console.log(xhr)
                     console.log('发送前')*/
                },
                success: function (data) {
                    console.log(data);
                    // if(data.result){
                    layer.msg("关联成功")
                    setTimeout(function () {
                        window.parent.location = window.parent.location.href

                    },1000)

                    // }else {
                    //     layer.msg("保存失败")
                    // }

                },
                error: function (xhr, textStatus) {
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete: function () {
                    //console.log('结束')
                }
            });

            return false;//禁止跳转，否则会提交两次，且页面会刷新

        });

        //表单初始赋值
        form.val('example', {
            "username": "贤心" // "name": "value"
            ,"password": "123456"
            ,"interest": 1
            ,"like[write]": true //复选框选中状态
            ,"close": true //开关状态
            ,"sex": "女"
            ,"desc": "我爱 layui"
        })


    });
</script>

</body>
</html>